<template>
  <div class="data">
      <section class="bg-purple" v-loading="loading">
        <div class="content" style="padding-left: 30px">
          <el-tree
            :data="categoryTree"
            show-checkbox
            default-expand-all
            :default-checked-keys="categoryData"
            node-key="idCategory"
            check-on-click-node
            ref="tree"
            :props="defaultProps">
          </el-tree>
        </div>
        <div class="btn-box">
          <el-button type="primary"
                     size="small"
                     :loading="saveTreeLoading"
                     @click="handleSave"
                     icon="el-icon-circle-plus-outline">保存</el-button>
        </div>
      </section>
  </div>
</template>

<script>

import { getSelfCategoryTree } from '../../../../api/category/category'

export default {
  name: 'dataPermissions',
  props: {
    type: {
      type: String,
      default: 'user'
    },
    saveTreeLoading: {
      type: Boolean,
      default: false
    },
    categoryData: {
      type: Array,
      default: () => ([])
    }
  },
  data () {
    return {
      loading: true,
      category: {},
      categoryTree: [],
      defaultProps: {
        children: 'children',
        label: 'name',
        value: 'idCategory'
      }
    }
  },
  mounted () {
    this.getSelectList()
  },
  watch: {
    categoryData () {
      this.getSelectList()
    }
  },
  methods: {
    getSelectList () {
      this.loading = true
      getSelfCategoryTree('sysModel', this.$route.query.isNotAjJnNode).then(res => {
        this.categoryTree = res.data.data
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleSave () {
      const ids = this.$refs.tree.getCheckedKeys()
      const info = {
        categoryIds: [],
        idData: ''
      }
      ids.map(item => {
        if (item || item !== 'null') {
          info.categoryIds.push(item)
        }
      })
      this.$emit('handleSaveCategory', info)
    }
  }
}
</script>

<style lang="scss" scoped>
  .data {
    height: 100%;
    background: #e9e9e9;
    padding: 15px;

    .bg-purple {
      background: #fff;
      height: 100%;
      border-radius: 5px;

      .content {
        height: calc(100vh - 160px);
        margin-bottom: 10px;
        padding-top: 10px;
        overflow-y: auto;
      }

      .btn-box {
        text-align: right;
        padding-right: 15px;
      }
    }

    .custom-title{
      .search-input{
        margin: 10px 0;
      }
    }
  }
</style>
